package com.siruier.boss.ui.activity.user

import android.graphics.Color
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import com.siruier.boss.R
import com.siruier.boss.api.UserIntegralBean
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.bean.CmTypeBean
import com.siruier.boss.common.formatPrice
import com.siruier.boss.databinding.ActivityWalletBinding
import com.siruier.boss.databinding.ItemWalletTypeBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseSignCheckAdapter
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.ext.*
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.helper.checkPayPasswordAndRealNameAuth
import com.siruier.boss.ui.utils.FullyGridLayoutManager
import com.siruier.boss.ui.widget.CommonButton

class WalletActivity : BaseActivity<ActivityWalletBinding>() {

    private lateinit var mRefreshHelper: RefreshHelper<UserIntegralBean>
    private val mWalletTypeAdapter = WalletTypeAdapter()
    private val mIncomeDetailAdapter = IncomeDetailAdapter()

    override fun initView() {
        mWalletTypeAdapter.datas = mutableListOf(
            CmTypeBean("挂卖金额", 1, 3, true),
            CmTypeBean("佣金金额", 2, 1),
            CmTypeBean("待结算金额", 3, 0),
        )
        mWalletTypeAdapter.setOnClickAdapterListenter(::changeType)
        vb.rvType.adapter = mWalletTypeAdapter
        vb.rvType.layoutManager = FullyGridLayoutManager(this, 3)
        (vb.rvType.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvDetail, mIncomeDetailAdapter, listener = ::onReload)
        changeType(null, 0)
    }

    private fun onReload() {
        val type = mWalletTypeAdapter.getCheck().type
        if (type == 3) {
            return
        }
        launchUI({
            when (type) {
                1 -> {
                    loginApi.balanceLog(null, null, mRefreshHelper.page).toData()?.records
                }
                2 -> {
                    loginApi.integralLog(null, null, mRefreshHelper.page).toData()?.records
                }
                else -> null
            }
        }, mRefreshHelper.handApiObserver())
    }

    private fun changeType(view: View?, position: Int) {
        val walletType = mWalletTypeAdapter.getItem(position)
        launchUI({
            when (view?.id) {
                R.id.button_withdrawal -> {
                    checkPayPasswordAndRealNameAuth()
                    startActivityExt<WithdrawalActivity> {
                        putExtra("busiType", walletType.typeNum)
                    }
                }
            }
            val type = walletType.type
            UserCache.getBalanceTotal().apply {
                var totalIntegralTitle = "总金额(￥)"
                var totalIntegral: String? = null
                var integral: String? = null
                var withdrawalAmount: String? = null
                when (type) {
                    1 -> {
                        totalIntegralTitle = "总金额(￥)"
                        totalIntegral = totalBalance.formatPrice()
                        integral = balance.formatPrice()
                        withdrawalAmount = withdrawalBalance.formatPrice()
                    }
                    2 -> {
                        totalIntegralTitle = "总金额(￥)"
                        totalIntegral = this.totalIntegral.formatPrice()
                        integral = this.integral.formatPrice()
                        withdrawalAmount = this.withdrawalAmount.formatPrice()
                    }
                    3 -> {
                        totalIntegralTitle = "待结算佣金总金额(元)"
                        totalIntegral = toBeIntegral.formatPrice()
                    }
                }
                vb.tvTotalMoney.textSpan("${totalIntegralTitle}\n${totalIntegral}") {
                    changeMoneyTextStyle(totalIntegral, this)
                }
                vb.tvWithdrawalAmount.isVisible = integral != null
                if (integral != null) {
                    vb.tvWithdrawalAmount.textSpan("可提现(￥)\n${integral}") {
                        changeMoneyTextStyle(integral, this)
                    }
                } else {
                    vb.tvWithdrawalAmount.text = ""
                }
                vb.tvOutWithdrawalAmount.isVisible = withdrawalAmount != null
                if (withdrawalAmount != null) {
                    vb.tvOutWithdrawalAmount.textSpan("已提现(￥)\n${withdrawalAmount}") {
                        changeMoneyTextStyle(withdrawalAmount, this)
                    }
                } else {
                    vb.tvOutWithdrawalAmount.text = ""
                }
                vb.llDetail.isVisible = type != 3
            }
            mRefreshHelper.onRefresh()
        }, ApiObserver())
    }

    private fun changeMoneyTextStyle(spanText: String?, span: TextViewSpan) {
        if (!spanText.isNullOrBlank()) {
            span.spanText = spanText
            span.addBoldSpan()
            span.addForegroundColorSpan(R.color.text_color.toColor())
            span.addAbsoluteSizeSpan(19.dp)
        }
    }

    class WalletTypeAdapter : BaseSignCheckAdapter<ItemWalletTypeBinding, CmTypeBean>() {
        override fun onBindViewHolder(vb: ItemWalletTypeBinding, bean: CmTypeBean, position: Int) {
            vb.buttonWithdrawal.isVisible = bean.type != 3
            vb.buttonInfo.isVisible = !vb.buttonWithdrawal.isVisible
            vb.tvTitle.text = bean.typeName
            if (bean.check) {
                vb.sl.setShadowColor(Color.TRANSPARENT)
                vb.tvTitle.setTextColor(R.color.white.toColor())
                vb.ivImage.setImageResource(R.mipmap.bg_wallet_check)
                vb.buttonWithdrawal.changeStyle(CommonButton.CB_STROKE_WHITE)
                vb.buttonInfo.setTextColor(R.color.white.toColor())
            } else {
                vb.sl.setShadowColor(R.color.shadow_color.toColor())
                vb.tvTitle.setTextColor(R.color.text_color.toColor())
                vb.ivImage.setImageResource(R.mipmap.bg_wallet_un_check)
                vb.buttonWithdrawal.changeStyle(CommonButton.CB_MALL_STROKE)
                vb.buttonInfo.setTextColor(R.color.text_hint_color_2.toColor())
            }
            setViewClick(position, vb.sl, vb.buttonWithdrawal)
        }
    }
}